package com.tt.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.tt.bean.Comment;
import com.tt.bean.SaySome;
import com.tt.utils.GetResult;
@Repository
public class CircleOfFriendsDaoImpl implements CircleOfFriendsDao {

	@Override
	public void sendMessage(SaySome saySome) {
		SqlSession ss = null;
		try {
			ss = GetResult.getSqlSession();
			long id = ss.selectOne("com.tt.bean.SaySomething.selectMaxID");
			saySome.setId(id);
			ss.insert("com.tt.bean.SaySomething.sendMessage", saySome);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
	}

	@Override
	public List<SaySome> selectSaySomething(HashMap<String, Long> hm) {
		SqlSession ss = null;
		List<SaySome> list = new ArrayList<>();
		try {
			ss = GetResult.getSqlSession();
			list = ss.selectList("com.tt.bean.SaySomething.selectSaySomething", hm);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
		return list;
	}

	@Override
	public void addGood(HashMap<String, Long> hm) {
		SqlSession ss = null;
		try {
			ss = GetResult.getSqlSession();
			ss.insert("com.tt.bean.SaySomething.addGood", hm);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
	}

	@Override
	public void removeGood(HashMap<String, Long> hm) {
		SqlSession ss = null;
		try {
			ss = GetResult.getSqlSession();
			ss.delete("com.tt.bean.SaySomething.removeGood", hm);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
	}

	@Override
	public long selectCount(HashMap<String, Long> hm) {
		SqlSession ss = null;
		long good = 0;
		try {
			ss = GetResult.getSqlSession();
			good = ss.selectOne("com.tt.bean.SaySomething.selectCount", hm);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
		return good;
	}

	@Override
	public int selectGood(HashMap<String, Long> hm) {
		SqlSession ss = null;
		int good = 0;
		try {
			ss = GetResult.getSqlSession();
			good = ss.selectOne("com.tt.bean.SaySomething.selectGood", hm);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
		return good;
	}

	@Override
	public void sendCommentAjax(Comment comment) {
		SqlSession ss = null;
		try {
			ss = GetResult.getSqlSession();
			Long id = ss.selectOne("com.tt.bean.SaySomething.selectCommentID");
			comment.setCid(id);
			ss.insert("com.tt.bean.SaySomething.sendCommentAjax", comment);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
	
	}

	@Override
	public List<Comment> selectComment(HashMap<String, Long> hm) {
		SqlSession ss = null;
		List<Comment> list = new ArrayList<>();
		try {
			ss = GetResult.getSqlSession();
			list = ss.selectList("com.tt.bean.SaySomething.selectComment", hm);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
		return list;
	}

}
